<template>
	<view>
		<view class="tuan-detail-header">
			<swiper :indicator-dots="true" indicator-color="rgba(255,255,255,.3)" indicator-active-color="#ffffff" class="tuan-detail-swiper" :autoplay="true"  :interval="3000" :duration="400">
				 <swiper-item>
					 <image :src="info.img" mode="aspectFill"></image>
				 </swiper-item>
			</swiper>
		</view>
		<view class="tuan-detail-tit pd20_15">
			<view class="ft18 cl-main ftw600">{{info.name}}</view>
			<view class="flex alcenter space mt12">
				<view class="flex alcenter">
					<text class="ml10 ft18 cl-orange">标准价：</text>
					<text class="ft18 cl-orange">¥{{info.price * 1}}</text>
					<text class="ft18 cl-orange">  </text>
				</view>
				<!-- <view class="cl-notice ft12">{{info.updateTime}}</view> -->
			</view>
			<view class="ml10 ft12 cl-notice">购买任意景点后，开放当前景区所有景点视频权限</view>
		</view>
		<view class="tuan-detail-content mt4">
			<view class="tuan-detail-content-tab bd-bottom">
					<sub-tab :tabs="tabs" :selectIndex="selectIndex" @change="changeIndex"></sub-tab>
			</view>
			<view v-if="selectIndex == 0" class="pd16_15">
				<view class="row" >
					<view style="display: flex;" v-for="(value,key) in info.videoList" :key="key">
						<view style="width: 100%;" class="mini-btn ft14" 
							:type="info.pid==value.id?'primary':'primary'" size="mini">{{value.name}}</view>
						<button v-if="isVideoFree(userPaidStatus,info.chargeStatus,value.chargeStatus)"
							@click="detail(info.id,value.id)"
							style="background: #e4e2e2; margin-top: -10upx; width: 180upx; height: 56upx; padding: 0upx 10upx; " 
							class="cl-orange"
							size="mini">{{userPaidStatus ? "点击观看":"免费观看"}}</button>
						<button v-else  @click="buys(value)" 
							style="background: #e4e2e2; margin-top: -10upx; width: 180upx; height: 56upx; padding: 0upx 10upx;" 
							size="mini">付费观看</button>
						<!-- 1.景区免费|| 景点免费 || 用户已付费-->
						<!-- <button v-if="!info.chargeStatus || !value.chargeStatus || userPaidStatus"
							@click="detail(info.id,value.id)"
							style="background: #e4e2e2; margin-top: -10upx; width: 180upx; height: 56upx; padding: 0upx 10upx; " size="mini">点击观看</button> -->
						<!-- 2.景区收费且用户未付费-->
						<!-- <button v-else-if="info.chargeStatus && !userPaidStatus" @click="buys(value)" 
							style="background: #e4e2e2; margin-top: -10upx; width: 180upx; height: 56upx; padding: 0upx 10upx;" 
							size="mini">购买</button> -->
					</view>
				</view>
			</view>
			<view v-if="selectIndex == 1" class="pd16_15">
				<view class="ft14 cl-main  lh20 mb16">
					<rich-text :nodes="info.descStr"></rich-text>
				</view>
			</view>
		</view>
		
		<home-default :datasa="datasa"></home-default>
		<!-- <view class="form-footer-h">
		</view> -->
		
		<dialog-login v-if="showLogin" @loginYes="loginYes" @closed="showLogin = false"></dialog-login>
		
	</view>
</template>

<script>
import zaudio from '@/components/uniapp-zaudio/zaudio';
// import zaudio from 'uniapp-zaudio/zaudio'
export default {
	components: { zaudio: zaudio },
		data(){
			return {
				isLogin:false,
				showLogin:false,
				showVideo:false,
				autoplay:false,
				playbtn:true,
				selectIndex:0,
				tabs:['目录','详情'],
				nextStep:'',
				datasa:[],
				dataconfig:[],
				id:'',
				mid:0,
				info:[],
				userPaidStatus:'',
				dda:'',
				price:'',
				banners:[],
				isplaytext:'已经购买',
				audioAction: {
				    method: 'pause'
				}
			}
		},
		onLoad(option){
			this.dda=uni.createVideoContext('myVideo')
			if(option.id){
				this.id=option.id
			}
			if(option.mid){
				this.mid=option.mid
			}
			// this.getList();
			
		},
		onShow() {
			
			if(uni.getStorageSync("userinfo").token){
				this.isLogin = true
				this.showLogin = false
			}
			
			this.getinfo(this.id);
			// 判断当前用户是否已经购买当前景区视频权限
			this.checkPaid(this.id);
			
			// this.banners=uni.getStorageSync("config").banner
		},
		onHide() {
			this.dda.pause()
		},
		onShareAppMessage(e){
			
		},
		onShareTimeline(e){
			
		},
		methods:{
			
			/**
			 * 判断景点是否免费
			 * @param {boolean} userPaidStatus - 是否付费过任意景点（关键全局状态）
			 * @param {boolean} areaChargeStatus - 景区状态：'收费'/'免费'
			 * @param {boolean} videoChargeStatus - 景点状态：'收费'/'免费'
			 * @returns {boolean} - true=免费 / false=收费
			 */
			isVideoFree(userPaidStatus, areaChargeStatus, videoChargeStatus) {
				console.log(userPaidStatus, areaChargeStatus, videoChargeStatus)
			  // 规则1：付费过任意景点 → 所有景点免费
			  if (userPaidStatus) return true;
			  
			  // 规则2：景区整体免费 → 所有景点免费
			  if (areaChargeStatus === false) return true;
			  
			  // 规则3：以上均不满足时，检查景点自身状态
			  return videoChargeStatus === false;
			},
			buys(value){
				let dataa = {};
				this.info.priced=value.price
				this.info.vippriced=value.vipprice
				this.info.pricedbuy=this.info.price
				if(uni.getStorageSync("userinfo").apiToken){
					if(this.info.isvip>1){
						this.info.pricedbuy=value.vipprice
					}
				}
				console.log(value)
				
				uni.setStorage({//缓存配置信息
					key: 'buydata',  
					data: this.info
				})
				uni.navigateTo({
					url:'/sub_pages/scenicArea/buy?id='+this.id+'&mid='+value.id
				})	
			},
			timeupdate(event){
					let _this=this;
					let currentTime = event.detail.currentTime 
					if(currentTime>this.info.mrseek && this.info.isplay==0){
						 //dda.exitFullScreen()
						 this.dda.seek(this.info.mrseek)
						 this.dda.pause()
						 this.dda.stop()
						 uni.showModal({
							title: '温馨提示',
							content: '没权限观看当前视频',
							showCancel: false,
							confirmText: "确定",
							success: function (res) {
								if (res.confirm) {
									
								} else if (res.cancel) {
									
								}
							}
						 });
					}
			},
			getinfo(id) {
				uni.showLoading({
				    title: '加载中'
				});
				let data = {};
				data.id=id
				data.mid=this.mid
				if(uni.getStorageSync("userinfo").token){
					data.token=uni.getStorageSync("userinfo").token
					this.isLogin = true
				}
				uni.request({
					url: this.configs.webUrl+'/api/scenicArea/'+id ,
					// data: data,
					success: data => {
							uni.hideLoading();
							if(uni.getStorageSync("userinfo").token){
								if(data.data.isvip>1){
									this.price=data.data.vipprice
								}else{
									this.price=data.data.price
								}
							}else{
								this.price=data.data.price
							}
							data.data.pricebuy=this.price
							this.info=[];
							this.info=data.data.data
							console.log('this.info')
							console.log(this.info)
							
							if(data.data.isvip>1){
								if(data.data.vipprice*1==0){
									this.isplaytext='VIP免费'
								}
							}else{
								if(data.data.price*1==0){
									this.isplaytext='免费视频'
								}
							}
							uni.setNavigationBarTitle({
							    title: this.info.name
							});
					},
					fail: (data, code) => {
						uni.hideLoading();
					}
				});
			},
			// 判断当前用户是否已经购买了当前景区的视频权限（购买任一视频权限，均开放当前景区内的所有视频权限）
			checkPaid(scenicId){
				var userinfo = uni.getStorageSync("userinfo");
				uni.request({
					url: this.configs.webUrl+'/api/scenicArea/checkPaid',
					data: {
						userId: userinfo.id,
						scenicAreaId: scenicId
					},
					method:'POST',
					
					success: data => {
						console.log("checkPaid");
						this.userPaidStatus = data.data.data;
						console.log(this.userPaidStatus);
						
						// this.checkShowVideo();
					},
					fail: (data, code) => {
					}
				});
			},
			checkShowVideo(){
				// 1.景区免费
				// 2.景区收费&景点免费
				// 3.景区收费&已付费
				this.showVideo = true;
			},
			// getList() { 
			// 	let data = {
			// 		//column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
			// 	};
			// 	var limit=10;
			// 	data.limit=limit
			// 	data.ishot=2
			// 	uni.request({
			// 		url: this.configs.webUrl+'/api/video/list',
			// 		data: data,
			// 		success: data => {
						
			// 			uni.setStorage({//缓存配置信息
			// 				key: 'config',  
			// 				data: data.data.config
			// 			})
			// 			if (data.data.total>0) {
			// 				this.dataconfig=data.data.config
			// 				this.datasa=data.data.rows
			// 			}
			// 		},
			// 		fail: (data, code) => {
			// 		}
			// 	});
			// },
			contactAct(){
				if(this.isLogin == false){
					this.showLogin = true;
					this.nextStep = 'contact';
				}else{
					uni.navigateTo({
						url:'/pages/client/vipcard/adviser'
					})
				}
			},
			vipcard(){
				uni.navigateTo({
					url:'/pages/client/vipcard/index'
				})
			},
			detail(id,mid){
				uni.navigateTo({
					url:'/sub_pages/scenicArea/detail?id='+id+'&mid='+mid
				})
				
				// console.log("收费："+this.info.chargeStatus)
				// if(this.info.chargeStatus & !this.userPaidStatus){
				// 	uni.navigateTo({
				// 		url:'/sub_pages/scenicArea/buy?id='+id+'&mid='+mid
				// 	})
				// }else{
				// 	uni.navigateTo({
				// 		url:'/sub_pages/scenicArea/detail?id='+id+'&mid='+mid
				// 	})
				// }
				
			},
			buyAct(){
				if(this.price*1==0){
					uni.showToast({ title: '免费视频不需要购买',icon:"none" });
					return false; 
				}
				if(this.isLogin == false){
					this.showLogin = true;
					this.nextStep = 'buy';
				}else{
					uni.setStorage({//缓存配置信息
						key: 'buydata',  
						data: this.info
					})
					uni.navigateTo({
						url:'/sub_pages/scenicArea/buy?id='+this.id
					})
				}
			},
			loginYes(){
				if(this.nextStep == 'buy'){
					uni.navigateTo({
						url:'/sub_pages/scenicArea/buy'
					})
				}else{
					uni.navigateTo({
						url:'/pages/client/vipcard/adviser'
					})
				}
			},
			changeIndex(index){
				this.selectIndex = index;
			}
		}
	}
</script>

<style>
	.user-not-vip{
		width: 100%;
		height: 80rpx;
		background: #FDF6EC;
		border-radius: 40rpx;
		border: 2rpx solid #EFC381;
		text-align: center;
		line-height: 76rpx;
		font-size: 24rpx;
		color:#000000;
		font-weight: bold;
	}
	
	.tuan-detail-header{
		position: relative;
	}
	.tuan-detail-swiper{
		height: 500rpx;
	}
	.tuan-detail-swiper image{
		width: 100%;
		height: 500rpx;
		background: #F2F2F2;
	}
	.tuan-detail-tit{
		width: 100%;
		background: #FFFFFF;
		border-radius: 40rpx 40rpx 0rpx 0rpx;
		position: relative;
		margin-top: 20rpx;
	}
	.tuan-detail-content-tab{
		height: 102rpx;
	}
	.tuan-detail-content{
		/* min-height: calc(100vh - 600rpx); */
		background: #FFFFFF;
	}
	.tuan-product-l{
		width: 320rpx;
		height: 240rpx;
		border-radius: 16rpx;
		background: #F2F2F2;
	}
	.tuan-product-r{
		width: calc(100% - 320rpx);
	}
	.cl-orange1{
		margin-right: 10upx;
		padding: 15upx;
		border: 1px solid #CCCCCC;
	}
	.mini-btn{
		margin-left: 10upx;
		margin-bottom: 20upx;
		height: 60upx;
		color: #666666;
		border-bottom: 1px solid #f0f0f0;
	}
</style>